package com.crm.system.service.impl.situationAware;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.crm.common.model.DhPageInfo;
import com.crm.common.util.*;
import com.crm.model.entity.companySpider.FinancialNews;
import com.crm.model.vo.situationAware.FinancialNewsVO;
import com.crm.model.vo.situationAware.IFengNewsVO;
import com.crm.model.vo.situationAware.SpiderDataVO;
import com.crm.service.situationAware.SpiderDbService;
import com.crm.system.service.situationAware.SpiderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author yuanyunfeng
 * @date 2021/6/3 9:23
 */
@Service
@Slf4j
public class SpiderServiceImpl implements SpiderService {

    @Autowired
    private SpiderDbService spiderDbService;


    @Override
    public DhPageInfo<SpiderDataVO> querySpiderData(Integer pageIndex, Integer pageSize, String sortName, String sortOrder, String companyName) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        List<SpiderDataVO> spiderDataList = spiderDbService.querySpiderData(companyName);
//        log.info("打印这个结果【{}】", new Gson().toJson(spiderDataList));
        PageInfo<SpiderDataVO> pageInfo = new PageInfo<>(spiderDataList);
        return new DhPageInfo<SpiderDataVO>(pageInfo, spiderDataList);
    }


    @Override
    public void insertFinancialNews(){
        long timeMillis = System.currentTimeMillis() / 1000;
        String url = "https://api3.finance.ifeng.com/live/getclsnew?beg=" + timeMillis + "&end=" + timeMillis;
        String stamp = timeMillis+"";
        String pageContent = WebUtil.getPageContent(url, null, null);
        String result = decodeUnicode(pageContent);
//        log.info("打印这个转码之后的结果是【{}】",result);
        String newStr = toJsonString(result);
//        log.info("打印最终转码之后的结果是【{}】",result);
        JSONObject datas = JSONObject.parseObject(newStr);
        JSONArray records = JSONObject.parseArray(datas.get("data").toString());
        List<IFengNewsVO> list = records.toJavaList(IFengNewsVO.class).stream().distinct().collect(Collectors.toList());
//        System.out.println("打印这个list："+new Gson().toJson(list));
        FinancialNews financialNews = null;
        for (IFengNewsVO ifeng : list) {
            financialNews = new FinancialNews();
            String newsCTime = ifeng.getCtime();
            financialNews.setNewsId(CommonUtils.getGUID());
            financialNews.setNewsDate(stampToDate(newsCTime));
            financialNews.setNewsTime(stampToTime(newsCTime));
            financialNews.setNewsContent(ifeng.getBrief());
            //关键字处理
            StringBuilder sb = new StringBuilder();
            String keyStr = ",";
            if (StringUtils.isNotBlank(ifeng.getBrief())) {
                KeyWordComputer key = new KeyWordComputer(5);
                Iterator it = key.computeArticleTfidf(ifeng.getBrief()).iterator();
                while (it.hasNext()) {
                    Keyword key2 = (Keyword) it.next();
                    sb.append(key2 + keyStr);
                }
            }
            keyStr = sb.toString();
            if (StringUtils.isNotBlank(keyStr)) {
                keyStr = keyStr.substring(0, keyStr.length() - 1);
                financialNews.setNewsKey(keyStr);
            }
            spiderDbService.insertFinancialNews(financialNews);
        }


    }


    @Override
    public DhPageInfo<FinancialNewsVO> getFinancialNews(Integer pageIndex, Integer pageSize, String sortName, String sortOrder, String newsDate) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        List<FinancialNewsVO> financialList = new ArrayList<>();
        List<FinancialNews> sqlList = spiderDbService.getFinancialNews(newsDate);
        for (FinancialNews sql : sqlList) {
            FinancialNewsVO financialNewsVO = new FinancialNewsVO();
            financialNewsVO.setNewsContent(sql.getNewsContent());
            financialNewsVO.setNewsDate(sql.getNewsDate());
            financialNewsVO.setNewsTime(sql.getNewsTime());
            financialNewsVO.setNewsKey(sql.getNewsKey());
            financialList.add(financialNewsVO);
        }
        financialList= financialList.stream().distinct().collect(Collectors.toList());
        log.info("打印这个结果【{}】", new Gson().toJson(financialList));
        PageInfo<FinancialNewsVO> pageInfo = new PageInfo<>(financialList);
        return new DhPageInfo<FinancialNewsVO>(pageInfo, financialList);
    }

    @Override
    public void updateKeyWords() {
        log.info("进入修改财经新闻网的关键字的方法·······");
        spiderDbService.updateKeyWords();
    }



    public static void main(String[] args) throws IOException {
        long timeMillis = System.currentTimeMillis() / 1000;
        System.out.println("打印当前时间戳："+timeMillis);
        String stamp = timeMillis+"";
        System.out.println("打印日期：" + stampToDate(stamp));
        System.out.println("打印时间："+stampToTime(stamp));
//        String url = "https://api3.finance.ifeng.com/live/getclsnew?beg="+timeMillis+"&end="+timeMillis;
        String url = "https://api3.finance.ifeng.com/live/getclsnew?beg=1624177260&end=1624177260";
        System.out.println("打印这个请求地址-----------"+url);
        String str = WebUtil.getPageContent(url, null, null);
        String result = decodeUnicode(str);
        JSONObject datas = JSONObject.parseObject(result);
        JSONArray records = JSONObject.parseArray(datas.get("data").toString());
        System.out.println("打印这个list:"+new Gson().toJson(records));
        List<IFengNewsVO> list = records.toJavaList(IFengNewsVO.class);
        System.out.println("打印这个===============:"+new Gson().toJson(list));

    }


    /**
     * 处理json字符串中value多余的双引号， 将多余的双引号替换为中文双引号
     * @param s
     * @return
     */
    private static String toJsonString(String s) {
        char[] tempArr = s.toCharArray();
        int tempLength = tempArr.length;
        for (int i = 0; i < tempLength; i++) {
            if (tempArr[i] == ':' && tempArr[i + 1] == '"') {
                for (int j = i + 2; j < tempLength; j++) {
                    if (tempArr[j] == '"') {
                        if (tempArr[j + 1] != ',' && tempArr[j + 1] != '}') {
                            tempArr[j] = '”'; // 将value中的 双引号替换为中文双引号
                        } else if (tempArr[j + 1] == ',' || tempArr[j + 1] == '}') {
                            break;
                        }
                    }
                }
            }
        }
        return new String(tempArr);
    }

    /*
     * 将时间戳转换为年月日
     */
    public static String stampToDate(String stamp) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(Long.valueOf(stamp)*1000);
        res = simpleDateFormat.format(date);
        return res;
    }

    /*
     * 将时间戳转换为时分秒
     */
    public static String stampToTime(String stamp) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
        Date date = new Date(Long.valueOf(stamp)*1000);
        res = simpleDateFormat.format(date);
        return res;
    }


    /**
     * http 请求数据返回 json 中中文字符为 unicode 编码转汉字转码
     *
     * @param theString
     * @return
     */
    public static String decodeUnicode(String theString) {
        char aChar;
        int len = theString.length();
        StringBuffer outBuffer = new StringBuffer(len);
        for (int x = 0; x < len; ) {
            aChar = theString.charAt(x++);
            if (aChar == '\\') {
                aChar = theString.charAt(x++);
                if (aChar == 'u') {
                    // Read the xxxx
                    int value = 0;
                    for (int i = 0; i < 4; i++) {
                        aChar = theString.charAt(x++);
                        switch (aChar) {
                            case '0':
                            case '1':
                            case '2':
                            case '3':
                            case '4':
                            case '5':
                            case '6':
                            case '7':
                            case '8':
                            case '9':
                                value = (value << 4) + aChar - '0';
                                break;
                            case 'a':
                            case 'b':
                            case 'c':
                            case 'd':
                            case 'e':
                            case 'f':
                                value = (value << 4) + 10 + aChar - 'a';
                                break;
                            case 'A':
                            case 'B':
                            case 'C':
                            case 'D':
                            case 'E':
                            case 'F':
                                value = (value << 4) + 10 + aChar - 'A';
                                break;
                            default:
                                throw new IllegalArgumentException(
                                        "Malformed   \\uxxxx   encoding.");
                        }

                    }
                    outBuffer.append((char) value);
                } else {
                    if (aChar == 't')
                        aChar = '\t';
                    else if (aChar == 'r')
                        aChar = '\r';
                    else if (aChar == 'n')
                        aChar = '\n';
                    else if (aChar == 'f')
                        aChar = '\f';
                    outBuffer.append(aChar);
                }
            } else
                outBuffer.append(aChar);
        }
        return outBuffer.toString();
    }



}
